package io.kiki.sba.registry.lifecycle;


public interface LifecycleState {

    boolean isEmpty();

    boolean isInitializing();

    boolean isInitialized();

    boolean isStarting();

    boolean isStarted();

    boolean isStopping();

    boolean isStopped();

    boolean isPositivelyStopped();

    boolean isDisposing();

    boolean isDisposed();

    boolean isPositivelyDisposed();

    LifecyclePhase getPhase();

    void setPhase(LifecyclePhase phase);

    /**
     * rollback to previous state
     *
     * @param e Exception
     */
    void rollback(Exception e);

    boolean canInitialize();

    boolean canStart();

    boolean canStop();

    boolean canDispose();

    enum LifecyclePhase {
        INITIALIZING {
            @Override
            LifecyclePhase next() {
                return INITIALIZED;
            }
        }, INITIALIZED {
            @Override
            LifecyclePhase next() {
                return STARTING;
            }
        }, STARTING {
            @Override
            LifecyclePhase next() {
                return STARTED;
            }
        }, STARTED {
            @Override
            LifecyclePhase next() {
                return STOPPING;
            }
        }, STOPPING {
            @Override
            LifecyclePhase next() {
                return STOPPED;
            }
        }, STOPPED {
            @Override
            LifecyclePhase next() {
                return DISPOSING;
            }
        }, DISPOSING {
            @Override
            LifecyclePhase next() {
                return DISPOSED;
            }
        }, DISPOSED {
            @Override
            LifecyclePhase next() {
                return INITIALIZING;
            }
        };

        abstract LifecyclePhase next();
    }
}
